package org.xtxg.inBoundHandler;

import java.util.logging.Logger;

import com.oracle.jrockit.jfr.Producer;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
import io.netty.util.CharsetUtil;

/**
 * 服务器的监听 处理器
 */
public class IOChandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf in = (ByteBuf) msg;
        System.out.println("服务端接收到信息 " + in.toString(CharsetUtil.UTF_8));// 将消息记录到控制台
        ctx.fireChannelRead(msg);
//        ctx.write(in); // 将接受到信息写个发送者，而不冲刷出站消息
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
//        ctx.writeAndFlush(Unpooled.EMPTY_BUFFER);
//                                            .addListener(ChannelFutureListener.CLOSE);// 将未决信息冲刷至远程节点，并且关闭Channel
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();// d打印异常栈
        ctx.close();// 关闭Channel
    }
}
